<?php $this->JQuery()->javascriptCaptureStart();?>
$j(document).ready(function() {
    $j('#validate_category').click(function() {
        var data = $j('#cat_form').serialize();
        $j.post('/admin/listing/update-category',data, function(data) {
                $j('<span/>').attr('id', 'saved').html('Saved').insertAfter($j('#validate_category')).fadeOut(2000, function() {
                    $j('#saved').remove();
                });
        });
        return false;
    });
    $j('.catlevel').each(function(index,ele) {
        initSelect(ele);
    });

    $j('.removecat').each(function(index, ele) {
        loadRemove(ele);
    });    

    $j('#add_category').click(function() {
        
        var total_cat_num = $j('#cat_num').val();
        
        var div = $j('<div/>');
        
        $j('<select/>').attr({
            id:'level1-'+total_cat_num,
            name:'level1['+total_cat_num+']',
            'class':'catlevel',
            rel:'level2-'+total_cat_num
        }).appendTo(div);
        
        $j('<select/>').attr({
            id:'level2-'+total_cat_num,
            name:'level2['+total_cat_num+']',
            'class':'catlevel',
            rel:'level3-'+total_cat_num
        }).appendTo(div);
        
        $j('<select/>').attr({
            id:'level3-'+total_cat_num,
            name:'level3['+total_cat_num+']',
            'class':'catlevel'
        }).appendTo(div);
        
        $j('<a/>').attr({
            href:'#',
            rel:total_cat_num,
            'class':'removecat'
        }).html('Remove Category')
        .appendTo(div);        
        
        div.insertBefore($j('#validate_category'));
        
        $j('.catlevel').each(function(index, ele) {
            initSelect(ele);
        });
        
        $j('.removecat').each(function(index, ele) {
            loadRemove(ele);
        });
        
        loadSelect($j('#level1-'+total_cat_num), 'level1-'+total_cat_num);
        
        $j('#cat_num').attr('value', parseInt(total_cat_num) + 1);
        return false;
    });
});

function initSelect(ele) 
{
    $j(ele).change(function() {
        loadSelect($j(this));
    });
}

function loadSelect(ele, target) {
    if(!target) {
        var target = $j(ele).attr('rel');
    }

    $j.get('/admin/listing/get-category-list', {val:$j(ele).val()}, function(data) {
        $j('#'+target).empty();
        if ($j(data.list).length > 0) {
            $j('#'+target).show();
        } else {
            $j('#'+target).hide();
        }

        $j('<option/>').attr({value:''}).html('--Select--').appendTo('#'+target);
        $j(data.list).each(function(index, ele) {
            $j('<option/>').attr({value:ele._id}).html(ele.name).appendTo('#'+target);
        });
    });
}

function loadRemove(ele) {
    $j(ele).click(function() {
        $j(this).parent().remove();
        $j('#cat_num').attr('value', $j('#cat_num').val() - 1);
        return false;
    });
}
<?php $this->JQuery()->javascriptCaptureEnd();?>

<?php $this->headStyle()->captureStart()?>
    #content td { padding:5px}
    #content table { border:1px #aaa solid; padding:5px; width:100%}
    #content th {font-weight:bold;}

<?php $this->headStyle()->captureEnd()?>
<table class="actions">
    <tr>
        <td>
            <a href="<?php echo $this->geturl('delete','listing', null, 'admin1', array('id'=> $this->listing->_id))?>">Delete</a>
        </td>
        <td>
            <a href="<?php echo $this->geturl('unpublish','listing', null, 'admin1', array('id'=> $this->listing->_id))?>">Unpublish</a>
        </td>
        <td>
            <a href="<?php echo $this->geturl('publish','listing', null, 'admin1', array('id'=> $this->listing->_id))?>">Publish</a>
        </td>
        <td>
            <a href="<?php echo $this->listing->url()?>" target="blank">Display in website</a>
        </td>
    </tr>
</table>
<table>
    <tr>
        <th>Category</th>
        <td>
            <div>
                <?php 
                    $cat_selected1 = $cat_selected2 = $cat_selected3 = '';
                    $cats = $this->listing->category;
                    
                ?>
                <?php if (!empty($this->listing->category)): ?>
                    <?php foreach((array)$cats as $k => $cat):?>                
                        <div>
                            <?php $tmp = $cat->getParentTree();?>
                            <?php foreach((array)$tmp as $k => $cat):?>
                                    <?php echo $cat->name?> &raquo;
                            <?php endforeach;?>
                        </div>
                    <?php endforeach;?>
                <?php endif;?>                
            </div>
            <form id="cat_form">
                <input type="hidden" name="id" value="<?php echo $this->listing->_id?>">
                <input type="hidden" name="cat_num" id="cat_num" value="<?php echo  count($cats)?>">

                <?php foreach($cats as $k => $ca): ?>
                    <div>
                        <select id="level1-<?php echo $k?>" name="level1[<?php echo $k?>]" class="catlevel" rel="level2-<?php echo $k?>">
                            <?php foreach($this->level1 as $cat):?>
                                <?php
                                    $selected = '';
                                    if (!empty($this->listing->category[$k]->path) && (string)$this->listing->category[$k]->path[0] == (string)$cat->_id) {
                                        $cat_selected1 = $this->listing->category[$k]->path[0];
                                        $selected = 'selected';
                                    }
                                ?>
                                <option value="<?php echo $cat->_id?>" <?php echo $selected?>><?php echo $cat->name?></option>
                            <?php endforeach;?>
                        </select>
                        <select id="level2-<?php echo $k?>" name="level2[<?php echo $k?>]" class="catlevel" rel="level3-<?php echo $k?>">
                            <?php if (!empty($cat_selected1)):?>
                                <?php foreach(Model_category::find($cat_selected1)->getChildren(false, array('sa' =>1))->sort(array('pos'=>1)) as $cat):?>
                                    <?php

                                        $selected = '';
                                        if ((string)$this->listing->category[$k]->path[1] == (string)$cat->_id) {
                                            $cat_selected2 = $this->listing->category[$k]->path[1];
                                            $selected = 'selected';
                                        }
                                    ?>   
                                    <option value="<?php echo $cat->_id?>" <?php echo $selected?>><?php echo $cat->name?></option>
                                <?php endforeach;?>
                            <?php endif;?>
                        </select>
                        <?php /*
                        <select id="level3-<?php echo $k?>" name="level3[<?php echo $k?>]" class="catlevel">
                            <?php if (!empty($cat_selected2)):?>
                                <?php foreach(Model_category::find($cat_selected2)->getChildren() as $cat):?>
                                    <?php
                                        $selected = '';
                                        if ((string)$this->listing->category[$k]->path[2] == (string)$cat->_id) {
                                            $cat_selected3 = $this->listing->category[$k]->path[2];
                                            $selected = 'selected';
                                        }
                                    ?>   
                                    <option value="<?php echo $cat->_id?>" <?php echo $selected?>><?php echo $cat->name?></option>
                                <?php endforeach;?>
                            <?php endif;?>
                        </select>
                        */?>
                        <a href="#" class="removecat" rel="<?php echo $k?>">Remove Category</a>
                    </div>
                <?php endforeach;?>                
                
                <button id="validate_category">
                    Validate
                </button>
                <a id="add_category" href="#">
                    Add a category
                </a>                
            </form>
        </td>
    </tr>
</table>
<table id="listings">
    <tr>
        <th>Username</th>
        <td><?php echo (!empty($this->listing->user->cid)) ? $this->listing->user->company->username : $this->listing->user->username?></td>
    </tr>
    <tr>
        <th>email</th>
        <td><?php echo $this->listing->user->email?></td>
    </tr>
</table>
<table>
    <tr>
        <th>Status</th>
        <td><?php echo $this->listing->sa?></td>
    </tr>
    <tr>
        <th>title</th>
        <td><?php echo $this->listing->title?></td>
    </tr>
    <?php if(!empty($this->listing->tl_tc)):?>
    <tr>
        <th>title_tc</th>
        <td><?php echo $this->listing->tl_tc?></td>
    </tr>
    <?php endif;?>
    <tr>
        <th>description</th>
        <td><?php echo $this->listing->description?></td>
    </tr>
    <?php if(!empty($this->listing->cs_tc)):?>
    <tr>
        <th>description_tc</th>
        <td><?php echo $this->listing->cs_tc?></td>
    </tr>
    <?php endif;?>
</table>
